<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../css/jquery.jsonview.css">
    <script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/commonutil.js"></script>
    <script type="text/javascript" src="../js/jquery.jsonview.js"></script>
    <script src="./function.js" charset="utf-8"></script>
    <style>
        .jsonview .null {
            color: red;
        }
        .jsonview .bool {
            color: #fde3a7;
        }
        .jsonview .num {
            color: #bf55ec;
        }
        .jsonview .string {
            color: #00b16a;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
<div class="edit-form-class" style="padding-top: 10px">
    <form class="layui-form" id="preViewForm">
        <div class="layui-form-item">
            <label class="layui-form-label">模板名称</label>
            <div class="layui-input-block">
                <input type="text" id="templateNo" name="templateNo" lay-verify="title" style="display: none" autocomplete="off" placeholder="" class="layui-input">
                <input type="text" id="templateName" name="templateName" lay-verify="title" readonly="readonly" style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">模板来源</label>
            <div class="layui-input-block">
                <input type="text" id="templateResource" name="templateResource" lay-verify="title" readonly="readonly" style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">签章类型</label>
            <div class="layui-input-block">
                <input type="text" id="sealType" name="sealType" lay-verify="title" readonly="readonly" style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公章规则</label>
            <div class="layui-input-block" id="officialSealId" style="width: 350px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请求数据</label>
            <div class="layui-input-inline">
                <textarea placeholder="请输入模板参数" oninput="OnInput(event)" class="layui-textarea" id="requestData" name="requestData" style="width: 350px;height: 130px"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请求数据</label>
            <div class="layui-input-inline">
                <div id="requestData2" style="border:1px solid #F34743;position:absolute; height:130px;width: 350px; overflow:auto"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="btnclass" style="padding-top: 110px;padding-left: 180px;">
                <div class="layui-btn" lay-submit="" autocomplete="off" lay-filter="submitBtn" id="preViewBtn">预览</div>
                <div id="closeWin" class="layui-btn layui-btn-primary" >取消</div>
            </div>
        </div>
    </form>
</div>
</body>
<script>
    layui.use(['laypage','jquery', 'layer', 'table', 'form', 'util','upload','layedit'], function(){
        let form = layui.form;

        let id = GetUrlParam("id");
        let url = serverPre + "/manager/scs/v2/template/one/"+id;
        let result = requestGet(url);
        buildOpenWin(result,form);

        //下载文件
        $('#downLoadFile').click(function () {
            exportTemplate(result.refId);
        });
    })
</script>
<script>
    function OnInput(event) {
        let value = event.target.value;
        if(value !== "" && value !== null){
            let json = JSON.parse(value);
            $("#requestData2").JSONView(json);
            $("#json-collapsed").JSONView(json, {collapsed: true,recursive_collapser: true });
        }
    }
    //取消
    $("#closeWin").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });
    //预览
    $("#preViewBtn").click(function () {
        //预览合同逻辑
        let url = serverPre + "/manager/signaturesync/v2/signature";
        let formData = serializeObject($("#preViewForm"));
        let needSign = true;
        if(formData.requestData == "" || formData.requestData == null){
            errorMsg("请求数据不能为空!");
            return false;
        }
        //如果certInfo为空，则不盖章
        var reqData = JSON.parse(formData.requestData);
        var certInfos = getJsonValue(reqData,"certInfos");
        var certApplys = getJsonValue(reqData,"certApplys");
        if((certInfos == null || certInfos == undefined) && (certApplys == null || certApplys == undefined)){
            needSign = false;
        }

        // let commitData = JSON.stringify({"templateNo":formData.templateNo,needSign:needSign,"sealType":formData.sealType,"requestData":formData.requestData});
        let commitData = JSON.stringify({"templateNo":formData.templateNo,needSign:needSign,"sealType":formData.sealType});
        let lookUrl;
        let fileUrl = getFileUrl(lookUrl,url,commitData);

        alert(fileUrl);
        // if(fileUrl !== null && fileUrl !=="" && fileUrl !== undefined){
        //     window.open(fileUrl,'_blank');
        // }
    });

    function getFileUrl(lookUrl,url,commitData) {
        let loading = layer.msg('正在生成合同...', {icon: 16, shade: 0.3, time: 0});
        $.ajax({
            url: url,
            async:false,
            data: commitData,
            type: 'POST',
            contentType: 'application/json',
            headers: {'token': getCookie('token')},
            success: function (res) {
                if (res !== "") {
                    layer.close(loading);
                    if(res.indexOf("msg") != -1 || res.indexOf("code") != -1){
                        errorMsg(res);
                    }else {
                        lookUrl = res;
                    }
                }else {
                    layer.close(loading);
                    alert("生成合同失败！");
                }
            },
            error: function (err) {
                errorMsg(err);
            }
        });
        return lookUrl;
    }
</script>
</html>
